/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *
 */
package crawler;

import db.MySQLDataManager;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.ParseException;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.ArrayList;

/**
*
* @author Vccorp
*/
public class getResultFrom_xoso88 {

     /* remove leading whitespace */
    public static String ltrim(String source) {
        return source.replaceAll("^\\s+", "");
    }

    public static String removeSpaces(String s) {
      StringTokenizer st = new StringTokenizer(s," ",false);
      String t="";
      while (st.hasMoreElements()) t += st.nextElement();
      return t;
    }

    public static boolean getResultFromBongDaCom()
    {
        boolean check = false;
        boolean check_giai_dau = false;
        String giai_dau = "";
        int count =0;
        ArrayList l = new ArrayList();
        try {
                //List l = new ArrayList();
                Map<String, ArrayList> bet_result = Collections.synchronizedMap(new HashMap<String, ArrayList>());
                //List l_other = new ArrayList();
                URL my_url = new URL("http://beta.bongda.com.vn/asianbookie/get.php?cid=ALL&straight=1");
                BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream()));
                String strTemp = "";
                //MySQLDataManager datamanager = new MySQLDataManager();
                while(null != (strTemp = br.readLine()))
                {
                    strTemp = ltrim(strTemp.replaceAll("( )+", " ")).replace("\"", " ");
                    //Debug.println(strTemp);
                    //neu gap giai dau thi lay het thong tin phia sau
                    if(strTemp.startsWith("<td colspan='2' rowspan='2' width='50%'>"))
                    {                        
                        giai_dau = strTemp.replaceAll("<td colspan='2' rowspan='2' width='50%'>", "").replaceAll("</td>","");
                        //da tim duoc giai dau
                        check_giai_dau = true;
                        ArrayList l1 = new ArrayList();
                        bet_result.put(giai_dau, l1);
                    }

                    if(check_giai_dau)
                    {                       
                        //lay thong tin ti le bong da
                        bet_result.get(giai_dau).add(strTemp);
                        //continue;
                    }
                }
                //xu li du lieu co trong map
                Set set = bet_result.entrySet();
                Iterator iter = set.iterator();
                while (iter.hasNext()) {
                  Map.Entry entry = (Map.Entry) iter.next();
                  //System.out.println(entry.getKey() + " -- " + entry.getValue());
                    for (int i=0;i<bet_result.get(entry.getKey()).size();i++)
                    {
                        Debug.println(bet_result.get(entry.getKey()).get(i).toString());
                    }
                  Debug.println("ngon");
                }

        }catch(Exception ex){
            System.out.println(ex.getMessage());
            Debug.println(ex.getMessage());
        }



      return check;
    }
    public static boolean insertResultInDB()
    {
        boolean check1 = false;
        boolean check1_other = false;
        boolean check2 = false;

        boolean check2_break_time1 = false;

        String quyen = null;
        int check_yes = 0;
        int check_yes_other = 0;
        int count = 0;
        String[] str = null;

        String row = "";
        String team1 = "";
        String team2 = "";

        String euro_1 = "";
        String euro_2 = "";
        String euro_3 = "";

        String asia_1 = "";
        String asia_2 = "";
        String asia_3 = "";

        String tx_1 = "";
        String tx_2 = "";
        String tx_3 = "";

        String hour = "";
        String day = "";

        Map m = new TreeMap();        
        Map<Integer, ArrayList> bet_result = Collections.synchronizedMap(new HashMap<Integer, ArrayList>());
        List l = new ArrayList();        
        List l_other = new ArrayList();
        
        for(int i=1; i <= 60; i++)
        {
            if((i == 1)||(i == 2)||(i == 3)||(i == 5)||(i == 6)||(i == 9)||(i == 55)||(i == 20)||(i == 30)||(i == 12)||(i == 6)||(i == 11)||(i == 7)||(i == 58)||(i == 60))
            {
                try {
                    //URL my_url = new URL("http://livescore.com/soccer/soccer/");
                    URL my_url = new URL("http://w2.sut.vn/ty-le-ca-cuoc-bong-da.aspx?league="+i);
                    Debug.println("http://w2.sut.vn/ty-le-ca-cuoc-bong-da.aspx?league="+i);
                    //URL my_url = new URL("http://w2.sut.vn/ket-qua-bong-da-truc-tuyen/england.aspx");
                    BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream()));
                    String strTemp = "";
                    while(null != (strTemp = br.readLine()))
                    {
                        strTemp = ltrim(strTemp.replaceAll("( )+", " ")).replace("\"", " ");
                        //Debug.println(strTemp);
/*
        //---------------------------------Ti le aisan/euro------------------------------------------------------------------------------
                        if(check2_break_time1)
                        {
                            check2_break_time1 = false;
                            break;
                        }

                        if(strTemp.contains("onclick= dhtmlwindow.open"))
                        {
                            check1 = true;
                            check_yes ++;
                        }

                        if(check1)
                        {
                            l.add(strTemp);
                            if(strTemp.contains("onclick= dhtmlwindow.open")&&(l.size() > 1))
                            {
                                int countTemp = 0;

                                for(int intIndex = 0; intIndex < l.size(); intIndex++)
                                {
                                    row = "";
                                    row = ltrim((String) l.get(intIndex));

                                    if(row.contains("Latest Over/Under (Total Goals) Odds"))
                                    {
                                        check2_break_time1 = true;
                                        break;
                                    }

                                    if(row.contains(",'top=100px,left=100px,width=700px,height=360px,resize=1,horizontalscrolling=no');return false;  href= #"))
                                    {
                                      Debug.println("team");
                                      String[] arr_team = row.substring(0, row.indexOf(",'top")).replaceAll("'", "").split("vs");
                                      team1 = arr_team[0];
                                      team2 = arr_team[1].replaceAll("'", "");
                                    }

                                    //get aisa_1
                                    if(row.startsWith("align=center nowrap><b>")||row.contains("src= http://imagescdn.asianbookie.com/arrow_down.gif  width= 9  height= 9  align= middle ><b>")||row.contains("src= http://imagescdn.asianbookie.com/arrow_up.gif  width= 9  height= 9  align= middle ><b>"))
                                    {
                                        Debug.println("asia_1");
                                        if(row.startsWith("src= http://imagescdn.asianbookie.com/arrow"))
                                        {
                                           String asia_1_temp = row.substring(row.indexOf("<b>"));
                                           asia_1 = asia_1_temp.replaceAll("<b>", "");
                                        }
                                        else
                                        {
                                            asia_1 = row.replaceAll("align=center nowrap><b>", "");
                                        }
                                    }

                                    //get asia_2
                                    if(row.contains("</b></font></td>"))
                                    {
                                        Debug.println("asia_2");
                                        String asia_2_temp = row.replaceAll("</b></font></td>", "");
                                        asia_2 = asia_2_temp.substring(asia_2_temp.indexOf(">")).replaceAll(">","");
                                    }

                                    //get asia_3
                                    if(row.contains("</b></td>") || (row.contains("</b><img")))
                                    {
                                        Debug.println("asia_3");
                                        if(row.contains("</b></td>"))
                                        {
                                            String asia_3_temp = row.replaceAll("</b></td>", "");
                                            asia_3 = asia_3_temp.substring(asia_3_temp.indexOf("<b>")).replaceAll("<b>","");
                                        } else{
                                            asia_3 = row.replaceAll("</b><img", "").replaceAll("align= center  nowrap><b>", "");
                                        }
                                    }

                                    //get euro 1
                                    if(row.contains("<b><font")&& row.contains("</font></b>"))
                                    {
                                        Debug.println("euro_1");
                                        String eur0_temp = row.replaceAll("<b><font", "").replaceAll("</font></b>", "");
                                        euro_1 = eur0_temp.substring(eur0_temp.indexOf(">")).replaceAll(">","");
                                    }

                                    //get euro 2
                                    if(row.contains("<font size") && row.contains("</font>"))
                                    {
                                        Debug.println("euro_2");
                                        countTemp ++;
                                        if(countTemp == 3)
                                        {
                                            String eur1_temp = row.replaceAll("<font size= 2 >", "").replaceAll("</font>", "");
                                            euro_2 = eur1_temp;
                                        }
                                        else if(countTemp == 4)
                                        {
                                            String eur1_temp = row.replaceAll("<font size= 2 >", "").replaceAll("</font>", "");
                                            euro_3 = eur1_temp;
                                        }
                                    }

                                    if(row.contains("&nbsp;&nbsp;") && row.contains("</font></td>"))
                                    {
                                        Debug.println("date");
                                         String time_temp = row.replaceAll("</font></td>", "");
                                         String[] time = time_temp.split("&nbsp;&nbsp;");
                                         hour   = time[1];
                                         day    = time[0];
                                    }
                                }
                                MySQLDataManager datamanager = new MySQLDataManager();
                                datamanager.insertResultInDB(team1, team2, euro_1, euro_2, euro_3, asia_1, asia_2, asia_3,"","","", hour, day, String.valueOf(i));
                                l.clear();
                            }
                        }
*/
        
        //---------------------------------Ti le tai/xiu------------------------------------------------------------------------------
                        if(strTemp.contains("top=100px,left=250px,width=700px,height=360px,resize=1,horizontalscrolling=no');return false;  href= #"))
                        {
                            check1_other = true;
                            check_yes_other ++;
                        }
                        if(check1_other)
                        {
                            l_other.add(strTemp);
                            if(strTemp.contains("top=100px,left=250px,width=700px,height=360px,resize=1,horizontalscrolling=no');return false;  href= #")&&(l_other.size() > 1))
                            {
                                //bet_result.put(check_yes, (ArrayList) l);
                                ArrayList l_temp = new ArrayList();
                                int countTemp1 = 0;
                                int countTemp2 = 0;

                                for(int intIndex = 0; intIndex < l_other.size(); intIndex++)
                                {
                                    String row1 = ltrim((String) l_other.get(intIndex));

                                    if(row1.contains(",'top=100px,left=250px,width=700px,height=360px,resize=1,horizontalscrolling=no');return false;  href= #"))
                                    {
                                      //String teamTemp = row1.replaceAll("'top=100px,left=100px,width=700px,height=360px,resize=1,horizontalscrolling=no'", "");
                                      String[] arr_team = row1.substring(0, row1.indexOf(",'top")).replaceAll("'", "").split("vs");
                                      team1 = arr_team[0];
                                      team2 = arr_team[1];
                                    }

                                    if(row1.contains("<b>")&& row1.startsWith("<b>"))
                                    {
                                        countTemp1 ++;
                                        if(countTemp1 == 1)
                                        {
                                            tx_1 = row1.replaceAll("<b>","");
                                        }
                                        else{
                                            continue;
                                        }
                                    }
                                    if(row1.contains("</b></font></td>"))
                                    {
                                        tx_2 = row1.replaceAll("</b></font></td>", "").replaceAll("size= 4 >","");
                                    }

                                    if(row1.contains("</b> </td>"))
                                    {
                                        countTemp2 ++;
                                        if(countTemp2 == 2)
                                        {
                                            tx_3 = row1.replaceAll("</b> </td>","");
                                        }else{
                                            continue;
                                        }
                                    }
                                }
                                MySQLDataManager datamanager = new MySQLDataManager();
                                datamanager.updateTX(team1, team2, tx_1, tx_2, tx_3);
                                l_other.clear();
                            }
                        }                        
                    }
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                    Debug.println(ex.getMessage());
                }
            }
            check1 = false;
            check2_break_time1 = false;
            l.clear();
        }
        
        return check1;
    }
    public static boolean getVideo()
    {
        boolean check = false;
        String catagory ="";
        String title ="";
        String embedd ="";
        String src ="";
        String duration ="";        
        String date ="";        
        String image ="";
          try {
            List l = new ArrayList();
            //List l_other = new ArrayList();
            URL my_url = new URL("http://clip360.net/");
            BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream()));
            String strTemp = "";
            MySQLDataManager datamanager = new MySQLDataManager();
            while(null != (strTemp = br.readLine()))
            {
                strTemp = ltrim(strTemp.replaceAll("( )+", " ")).replace("\"", " ");               

                //get all link 
                if(strTemp.startsWith("<li class= cat-item cat-item"))
                {                    
                    String link = strTemp.substring(strTemp.indexOf("href"),strTemp.indexOf("title")).replaceAll("href=","");
                    //Debug.println(link);
                    URL my_url_1 = new URL(link);
                    BufferedReader br_1 = new BufferedReader(new InputStreamReader(my_url_1.openStream()));
                    String strTemp_1 = "";
                    
                    //enter each link main page
                        try{
                            while(null != (strTemp_1 = br_1.readLine()))
                            {
                                strTemp_1 = ltrim(strTemp_1.replaceAll("( )+", " ")).replace("\"", " ");                               
                                if(strTemp_1.startsWith("<div class= pagination clearfix"))
                                {
                                    Debug.println(strTemp_1);
                                    String max = strTemp_1.substring(strTemp_1.indexOf("'>&raquo;</a></div>")-10,strTemp_1.indexOf("'>&raquo;</a></div>"));
                                    String max1 = max.substring(max.indexOf("ge/")).replaceAll("ge/","");
                                    int max_value = Integer.valueOf(max1);
                                    //Debug.println(String.valueOf(max_value));
                                    //get content sub page
                                    for(int i = 2;i<=max_value ;i++)
                                    {                                            
                                        URL my_url_sub = new URL(link+"/page/"+i);
                                        BufferedReader br_sub = new BufferedReader(new InputStreamReader(my_url_sub.openStream()));
                                        String strTemp_sub = "";
                                        try {
                                            while(null != (strTemp_sub = br_sub.readLine()))
                                            {
                                                    strTemp_sub = ltrim(strTemp_sub.replaceAll("( )+", " ")).replace("\"", " ");
                                                    
                                                    if(strTemp_sub.startsWith("<h2><a href="))
                                                    {
                                                            String link1 = strTemp_sub.substring(strTemp_sub.indexOf("href"),strTemp_sub.indexOf("title")).replaceAll("href=","");
                                                            //Debug.println(link1);
                                                            //get content
                                                            try {
                                                                URL my_url_2 = new URL(link1);
                                                                BufferedReader br_2 = new BufferedReader(new InputStreamReader(my_url_2.openStream()));
                                                                String strTemp_2 = "";
                                                                while(null != (strTemp_2 = br_2.readLine()))
                                                                {
                                                                    strTemp_2 = ltrim(strTemp_2.replaceAll("( )+", " ")).replace("\"", " ");
                                                                    //Debug.println(strTemp_2);
                                                                    if(strTemp_2.startsWith("Tiêu đề:"))
                                                                    {
                                                                        //Debug.println(strTemp_2);
                                                                        title = strTemp_2.replaceAll("Tiêu đề: <b>", "").replaceAll("</b><br />", "");
                                                                        //Debug.println("title:"+title);
                                                                    } else if(strTemp_2.startsWith("Thời lượng:"))
                                                                    {
                                                                        duration = strTemp_2.replaceAll("Thời lượng: <b>", "").replaceAll("</b><br />", "");
                                                                        //Debug.println("duration:"+duration);
                                                                    }else if(strTemp_2.startsWith("Chuyên mục:"))
                                                                    {
                                                                        catagory = strTemp_2.substring(strTemp_2.indexOf("title="), strTemp_2.indexOf(" rel")).replaceAll("title= ", "");
                                                                        //Debug.println("loai:"+catagory);
                                                                    }else if(strTemp_2.startsWith("Nguồn:"))
                                                                    {
                                                                        src= strTemp_2.replaceAll("Nguồn:", "").replaceAll("title=","");
                                                                        //Debug.println("src:"+src);
                                                                    }else if(strTemp_2.startsWith("<iframe"))
                                                                    {
                                                                        embedd = strTemp_2;
                                                                        //Debug.println("embedd:"+embedd);
                                                                    }else if(strTemp_2.startsWith("<img src")&&strTemp_2.contains("http://i.ytimg.com"))
                                                                    {
                                                                        image = strTemp_2.substring(strTemp_2.indexOf("<img src="), strTemp_2.indexOf("title")).replaceAll("<img src=", "");
                                                                        //Debug.println("image:"+image);
                                                                    }
                                                                }
                                                                //datamanager.insertVideo(catagory, title, embedd, src, image,duration);
                                                                Debug.println("ok");
                                                            }catch (Exception ex)
                                                            {
                                                                System.out.println(ex.getMessage());
                                                                Debug.println(ex.getMessage());
                                                            }
                                                        //end get content
                                                    }                                                
                                        }
                                        }catch(Exception ex){
                                            System.out.println(ex.getMessage());
                                            Debug.println(ex.getMessage());
                                        }
                                    }                                    
                                }
                                
                                if(strTemp_1.startsWith("<h2><a href="))
                                {
                                        String link1 = strTemp_1.substring(strTemp_1.indexOf("href"),strTemp_1.indexOf("title")).replaceAll("href=","");
                                        //Debug.println(link1);

                                        //get content
                                        try {
                                            URL my_url_2 = new URL(link1);
                                            BufferedReader br_2 = new BufferedReader(new InputStreamReader(my_url_2.openStream()));
                                            String strTemp_2 = "";
                                            while(null != (strTemp_2 = br_2.readLine()))
                                            {
                                                strTemp_2 = ltrim(strTemp_2.replaceAll("( )+", " ")).replace("\"", " ");
                                                //Debug.println(strTemp_2);
                                                if(strTemp_2.startsWith("Tiêu đề:"))
                                                {
                                                    //Debug.println(strTemp_2);
                                                    title = strTemp_2.replaceAll("Tiêu đề: <b>", "").replaceAll("</b><br />", "");
                                                    //Debug.println("title:"+title);
                                                } else if(strTemp_2.startsWith("Thời lượng:"))
                                                {
                                                    duration = strTemp_2.replaceAll("Thời lượng: <b>", "").replaceAll("</b><br />", "");
                                                    //Debug.println("duration:"+duration);
                                                }else if(strTemp_2.startsWith("Chuyên mục:"))
                                                {
                                                    catagory = strTemp_2.substring(strTemp_2.indexOf("title="), strTemp_2.indexOf(" rel")).replaceAll("title= ", "");
                                                    //Debug.println("loai:"+catagory);
                                                }else if(strTemp_2.startsWith("Nguồn:"))
                                                {
                                                    src= strTemp_2.replaceAll("Nguồn:", "").replaceAll("title=","");
                                                    //Debug.println("src:"+src);
                                                }else if(strTemp_2.startsWith("<iframe"))
                                                {
                                                    embedd = strTemp_2;
                                                    //Debug.println("embedd:"+embedd);
                                                }else if(strTemp_2.startsWith("<img src")&&strTemp_2.contains("http://i.ytimg.com"))
                                                {
                                                    image = strTemp_2.substring(strTemp_2.indexOf("<img src="), strTemp_2.indexOf("title")).replaceAll("<img src=", "");
                                                    //Debug.println("image:"+image);
                                                }                                                
                                            }
                                            //datamanager.insertVideo(catagory, title, embedd, src, image,duration);
                                            Debug.println("ok");
                                        }catch (Exception ex)
                                        {
                                            System.out.println(ex.getMessage());
                                            Debug.println(ex.getMessage());
                                        }
                                    //end get content
                                }
                            }
                             Debug.println("ok");
                       }catch (Exception ex) {
                            System.out.println(ex.getMessage());
                            Debug.println(ex.getMessage());
                        }
                }
            }
          }catch (Exception ex) {
            System.out.println(ex.getMessage());
            Debug.println(ex.getMessage());
        }               
        return check;
            
    }
    public static boolean getResultFootballFromBongda()
    {
        boolean check = false;
        
        String arr[] = new String[1000];

          try {
            List l = new ArrayList();
            //List l_other = new ArrayList();
            URL my_url = new URL("http://www.bongda.com.vn/ketqua/");
            BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream()));
            String strTemp = "";

            while(null != (strTemp = br.readLine()))
            {
                strTemp = ltrim(strTemp.replaceAll("( )+", " ")).replace("\"", " ");
                Debug.println(strTemp);
                /*
                if(strTemp.startsWith("<span id= ctl00_ContentPlaceHolder1_Label1"))
                {
                    //get data
                    arr = strTemp.split("<td class= title  colspan= 4  height= 18 >");
                    //Debug.println(strTemp);
                    //insertResultFootballinDB(arr);
                }*/
            }
            /*
            Debug.println(arr[4]);
            for (int i = 0;i < arr.length; i++)
            {
                if(i != 0)
                {
                    insertResultFootballinDB(arr[i]);
                }
            }*/

          }catch (Exception ex) {
                System.out.println(ex.getMessage());
                Debug.println(ex.getMessage());
         }

        
        return check;
    }
    public static boolean getResultFootball()
    {
        boolean check = false;        
        String arr[] = new String[1000];
       
          try {
            List l = new ArrayList();
            //List l_other = new ArrayList();
            URL my_url = new URL("http://w2.sut.vn/");
            BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream()));
            String strTemp = "";
            
            while(null != (strTemp = br.readLine()))
            {
                strTemp = ltrim(strTemp.replaceAll("( )+", " ")).replace("\"", " ");
                if(strTemp.startsWith("<span id= ctl00_ContentPlaceHolder1_Label1"))
                {
                    //get data
                    arr = strTemp.split("<td class= title  colspan= 4  height= 18 >");
                    //Debug.println(strTemp);
                    //insertResultFootballinDB(arr);
                }                                
            }
            Debug.println(arr[4]);
            for (int i = 0;i < arr.length; i++)
            {
                if(i != 0)
                {
                    insertResultFootballinDB(arr[i]);
                }
            }

          }catch (Exception ex) {
                System.out.println(ex.getMessage());
                Debug.println(ex.getMessage());
         }
         return check;
    }
    public static boolean insertResultFootballinDB(String str) throws ParseException
    {
        boolean check = false;
        String data = "";
        String splitdata1[] = new String[1000];
        String splitdata2[] = new String[1000];
        String splitdata3[] = new String[1000];
        String splitdata4[] = new String[1000];
        String splitdata5[] = new String[1000];
        String splitdata6[] = new String[1000];
        String str_match[] = new String[1000];
        String date_value[] = new String[3];

        MySQLDataManager datamanager = new MySQLDataManager();
        //process data       
        data = str.replaceAll("&nbsp;", "");
        splitdata1 = data.split("</td></tr><tr bgcolor= #333333 ><td class= match-light  width= 45  height= 18 >");       
        String giai_dau = splitdata1[0].replaceAll("</b>", "").replaceAll("<b>", "");
        
        splitdata2 = splitdata1[1].split("</td><td class= match-light  align= right  width= 423  colspan= 3 >");
        String time_country = splitdata2[0];

        str_match = splitdata2[1].split("<td width= 45  height= 18 >");
        
        String date = str_match[0].substring(0,str_match[0].indexOf("</td></tr><tr><td"));
        date_value = date.split(" ");
        long start_time = convertDatetoUnixTime(date_value[1],date_value[0]);

        for (int i =1 ;i < str_match.length ;i++)
        {                      
            splitdata4 = str_match[i].split("</td><td align= right  width= 186 >");
            String check_finish =  splitdata4[0];

            splitdata5 = splitdata4[1].split("</td><td");
            String team1  =  splitdata5[0];
            String team2 ="";
            String team_win ="";
            String ti_so = "";
            //get ti so
            if((check_finish == null ? "FT" == null : check_finish.equals("FT")) || (check_finish == null ? "Pen." == null : check_finish.equals("Pen.")))
            {
                if(splitdata5[1].contains("</a>"))
                {
                    ti_so = splitdata5[1].substring(splitdata5[1].length()-9,  splitdata5[1].length()).replaceAll("</a>", "");
                    splitdata6 = splitdata5[2].split("</td></tr><tr><td");
                    team2 = splitdata6[0].replaceAll("width= 186 >", "");
                    String[] arrtiso = new String[100];
                    arrtiso = ti_so.split(" - ");

                    if(Integer.valueOf(ltrim(arrtiso[0])) > Integer.valueOf(ltrim(arrtiso[1])))
                    {
                        team_win = team1;
                    } else if (Integer.valueOf(ltrim(arrtiso[0])) < Integer.valueOf(ltrim(arrtiso[1])))
                    {
                        team_win = team2;
                    } else{
                        team_win = "hoa";
                    }

                    Debug.println(giai_dau +":"+time_country+":"+date+":"+check_finish+":"+team1+":"+ti_so+":"+team2+":"+start_time);
                    datamanager.insertResultFoolBall(team1, team2, ti_so, team_win, "1", "", giai_dau,start_time);
                    check = true;
                } else{
                    ti_so = splitdata5[1].replaceAll("align= center  width= 51 >", "");
                    //datamanager.insertResultFoolBall(team1, team2, ti_so, team_win, "0", "", giai_dau);
                }
            }
           //Debug.println(giai_dau +":"+time_country+":"+date+":"+check_finish+":"+team1+":"+ti_so+":"+team2);
            
        }
        return check;
    }    
    public static long convertDatetoUnixTime(String day, String month) throws ParseException
    {
        long value = 0;
        int month_value = convertMonth(month);
        int year_value = TimeUtil.getYear();
        String date_value = day+"/"+String.valueOf(month_value)+"/"+String.valueOf(year_value);
        Debug.println(date_value);
        value = TimeUtil.longToDate(date_value);
        return value;
    }

    public static int convertMonth(String day)
    {
        int value = 0;
        String arr[] = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        for (int i = 0; i < arr.length; i++)
        {
            if(arr[i].contains(day))
            {
                value = i+1;
                break;
            }
        }
        return value;
    }
    /*
     * Lay cac keo da danh ma tran dau da ket thuc trong ngay hom nay
     * Tiep do tim xem co ket qua nao da ket thuc trong ngay hom nay chua
     * neu co thi tinh toan ket qua va update coin 
     * Neu khong thi ket thuc
     */
    public static boolean processData() throws ParseException
    {
        boolean val_return = false;
        //get current date no have hour
        long current_date = TimeUtil.longToDate(TimeUtil.getCurrentTimeNoHour());
        System.out.println(current_date);
        //Lay cac keo da danh ma co cac tran dau ket thuc trong 3 ngay
            MySQLDataManager datamanager = new MySQLDataManager();
            Set bet_results = new HashSet();
            bet_results = datamanager.getBetByOpenTime(current_date);
        //lay tung keo cua cac tran da dat
            Iterator it = bet_results.iterator();
            while (it.hasNext()) {
                // Get element trong bang betting
                BetProperties element = (BetProperties) it.next();
                String[] arr = new String[100];
                //handle data
                System.out.println(element.vs);
                
                if(element.vs.contains("  vs ")){
                     arr = element.vs.split("  vs ");
                }else {
                     arr = element.vs.split(" vs ");
                }
               
                String team1 = arr[0];
                String team2 = arr[1];
                //Lay ket qua cua tran neu co
                if(datamanager.checkExistResultFB(team1, team2, current_date))
                {
                    String[] scorearr = new String[10];
                    List results = new ArrayList();
                    results = datamanager.getResultFB(team1, team2, current_date);
                    String score = (String) results.get(1);
                    String team_win = (String) results.get(2);
                    scorearr = score.split(" - ");
                    //get category to count 
                    if(element.category_id == 1)
                    {
                        handleDataTypeEuro(element.id, element.user_id, element.team_selected, team_win, element.rate_selected, element.price);                        
                    } else if(element.category_id == 2){
                        int diff = Math.abs(Integer.valueOf(scorearr[0]) - Integer.valueOf(scorearr[1]));
                        //lay doi yeu hon va lay ti le cao hon ,vi du 0:2 1/4 thi stbuff co dang Spain-2 1/4
                        //ti le 0:0 (truong hop dac biet)
                        // neu hai doi hoa thi tra lai tien
                        // neu doi nao thang thi ben do an
                        if(element.rate_add == null ? "0 : 0" == null : element.rate_add.equals("0 : 0"))
                        {
                            if(scorearr[0] == null ? scorearr[1] == null : scorearr[0].equals(scorearr[1]))
                            {
                                MySQLDataManager datamangr = new MySQLDataManager();
                                datamangr.updateCoinToTableBet(element.id, element.price, 1);
                                datamangr.updateCoinToTableUser(element.user_id, element.price, 1);
                                continue;
                            } else {
                                if(Integer.valueOf(scorearr[0]) > Integer.valueOf(scorearr[1]))
                                {
                                    if(element.team_selected == null ? team1 == null : element.team_selected.equals(team1))
                                    {
                                        MySQLDataManager datamangr = new MySQLDataManager();
                                        datamangr.updateCoinToTableBet(element.id, (int) (element.price * element.rate_selected), 1);
                                        datamangr.updateCoinToTableUser(element.user_id, (int) (element.price * element.rate_selected), 1);                                        
                                    }else{
                                        MySQLDataManager datamangr = new MySQLDataManager();
                                        datamangr.updateCoinToTableBet(element.id, - element.price, -1);
                                        datamangr.updateCoinToTableUser(element.user_id, 0, -1);                                        
                                    }
                                } else{
                                    if(element.team_selected == null ? team2 == null : element.team_selected.equals(team2))
                                    {
                                        MySQLDataManager datamangr = new MySQLDataManager();
                                        datamangr.updateCoinToTableBet(element.id, (int) (element.price * element.rate_selected), 1);
                                        datamangr.updateCoinToTableUser(element.user_id, (int) (element.price * element.rate_selected), 1);                                        
                                    }else{
                                        MySQLDataManager datamangr = new MySQLDataManager();
                                        datamangr.updateCoinToTableBet(element.id, - element.price, -1);
                                        datamangr.updateCoinToTableUser(element.user_id, 0, -1);                                        
                                    }
                                }
                                continue;
                            }
                        }
                        StringBuffer stbuff = getWeakTeamAndHigherRate(arr, element.rate_add);


                        String[] arrbuff = new String[10];
                        arrbuff = stbuff.toString().split("-");
                        handleDataTypeAsian(element.id, element.user_id, element.team_selected, team_win, element.rate_selected, element.price, diff,ltrim(arrbuff[0]),arrbuff[1]);
                    } else if (element.category_id == 3){
                        int total = Integer.valueOf(scorearr[0]) +Integer.valueOf(scorearr[1]);
                        handleDataTypeTX(element.id, element.user_id, element.team_selected, team_win, element.rate_selected, element.price, total,element.rate_add);
                    } else if(element.category_id == 4){
                        
                    }

                }
            }        
        return val_return;
    }

    public static boolean handleDataTypeEuro(int bet_id , int user_id, String team_selected,String team_win,
                                                                        float rate_selected, int price){
        boolean val_return = false;
        int is_increase = 0;
        float profit = 0;
        MySQLDataManager datamanager = new MySQLDataManager();
        //if win
        if(team_win == null ? team_selected == null : team_win.equals(team_selected))
        {
            is_increase = 1;
            profit = rate_selected * price;
            datamanager.updateCoinToTableBet(bet_id, (int) profit, is_increase);
            datamanager.updateCoinToTableUser(user_id, (int) profit, is_increase);
        } else{
            //if lost
            is_increase = -1;
            profit = - price;
            //update vao de biet thang hay thua
            datamanager.updateCoinToTableBet(bet_id, (int) profit, is_increase);
            //Do da tru tien truoc cua user len neu user thua thi cong vao 0
            datamanager.updateCoinToTableUser(user_id, 0, is_increase);
        }

        return val_return;

    }
   
    public static boolean handleDataTypeAsian(int bet_id , int user_id, String team_selected, 
                                                    String team_win,float rate_selected, int price,int diff ,
                                                                            String weakteam,String higherscore){
        boolean val_return = false;
        int value = getPriceAsian(bet_id, user_id, team_selected, team_win, rate_selected, price, diff, higherscore,weakteam);
        
        MySQLDataManager datamanager = new MySQLDataManager();
        
        if(value > 0)
        {
            datamanager.updateCoinToTableBet(bet_id, value, 1);
            datamanager.updateCoinToTableUser(user_id, value, 1);
        } else {
            datamanager.updateCoinToTableBet(bet_id, value, -1);
            datamanager.updateCoinToTableUser(user_id, 0, -1);
        }
        val_return = true;
        
        return val_return;
    }
    
    public static int getPriceAsian(int bet_id , int user_id, String team_selected, String team_win,
                                                float rate_selected, int price,int diff ,String higherscore,String weakteam){
        int valReturn = 0;
        String first[] = new String[] {"0","1", "2", "3", "4", "5", "6", "7", "8"};
        String second[] = new String[] {"", " 1/4", " 1/2", " 3/4"};
        //handle with 1/4,1/2,3/4
        if(higherscore == null ? "1/4" == null : higherscore.equals("1/4"))//hoa thua nua
        {            
            if(diff == 0)
            {
                //Neu chon doi manh hon thi thua nua
                    if(team_selected == null ? weakteam == null : team_selected.equals(weakteam))
                    {
                       return (int) (price + price/2);
                    } 
                    //thua nua su
                    else{
                        return price/2;
                    }                

            } else if(diff >= 1){
                if(team_selected == null ? team_win == null : team_selected.equals(team_win))
                {
                    /* chon doi thang
                    * Thang khi diff > 0 (cach biet mot ban)                   
                    */
                    return (int) (price * rate_selected);

                } else if(team_selected == null ? team_win != null : !team_selected.equals(team_win)){
                    //thua
                    return -price;
                }
            }
        } else if(higherscore == null ? "1/2" == null : higherscore.equals("1/2"))
        {
            if(diff == 0)
            {
                if(team_selected == null ? team_win == null : team_selected.equals(team_win))
                {
                    //thua ca
                    return -price;

                }
                else if(team_win == null ? "hoa" == null : team_win.equals("hoa"))
                {
                    if(team_selected == null ? weakteam == null : team_selected.equals(weakteam))
                    {
                       return (int) (price * rate_selected);
                    }
                    //Thua ca tien
                    else{
                        return -price;
                    }
                }
                else if(team_selected == null ? team_win != null : !team_selected.equals(team_win)){
                    //thang ca
                    return (int) (price * rate_selected);
                }
            } else if(diff >= 1){
                if(team_selected == null ? team_win == null : team_selected.equals(team_win))
                {
                    /* chon doi thang
                    * Thang khi diff > 0 (cach biet mot ban)
                    */
                    return (int) (price * rate_selected);

                } else if(team_selected == null ? team_win != null : !team_selected.equals(team_win)){
                    //thua ca
                    return -price;
                }
            }

        }else if(higherscore == null ? "3/4" == null : higherscore.equals("3/4")){
            if(diff == 0)
            {
                if(team_selected == null ? team_win == null : team_selected.equals(team_win))
                {
                    //neu hoa la thua
                    return -price;
                }
                else if(team_win == null ? "hoa" == null : team_win.equals("hoa"))
                {
                    if(team_selected == null ? weakteam == null : team_selected.equals(weakteam))
                    {
                       return (int) (price * rate_selected);
                    }
                    //thua ca tien
                    else{
                        return -price;
                    }
                }
                else if(team_selected == null ? team_win != null : !team_selected.equals(team_win)){
                    //neu hoa la thang
                    return (int) (price * rate_selected);
                }
                // thieu phan hoa neu chon doi yeu hon thi thang ca su

                //code here

            } else if(diff == 1){
                if(team_selected == null ? team_win == null : team_selected.equals(team_win))
                {
                    //thang nua
                    return (int) (price + price/2);

                } else if(team_selected == null ? team_win != null : !team_selected.equals(team_win)){
                    //thua nua
                    return -price/2;
                }
            }else if(diff > 1){
                if(team_selected == null ? team_win == null : team_selected.equals(team_win))
                {
                    /* chon doi thang
                    * Thang khi diff > 0 (cach biet mot ban)
                    */
                    return (int) (price * rate_selected);

                } else if(team_selected == null ? team_win != null : !team_selected.equals(team_win)){
                    //thua
                    return -price;
                }
            }
        }

        //handle others
        for (int i = 0 ; i< first.length ; i++)
        {
            for(int j = 0 ; j< second.length ; j++)
            {
                if(higherscore == null ? first[i] + second[j] == null : higherscore.equals(first[i] + second[j]))
                {
                    int first_par = Integer.valueOf(first[i]);
                    //thang neu
                    if(second[j] == null ? "" == null : second[j].equals("")){ // ex. 2

                        //Truong hop doi yeu hon thang
                        if(team_win == null ? weakteam == null : team_win.equals(weakteam))
                        {
                            if(team_selected == null ? team_win == null : team_selected.equals(team_win))
                            {
                                return (int) (price * rate_selected);
                            } else{
                                return -price;
                            }
                        }
                        
                        if(team_selected == null ? team_win == null : team_selected.equals(team_win)){  //neu chon doi thang
                            if(diff > first_par)
                            {
                                //thang ca
                                return (int) (price * rate_selected);
                            } else if (diff == first_par) {
                                //hoa tien
                                return price;
                            } else if (diff < first_par) {
                                //thua tien
                                return -price;
                            }

                        } else if(team_selected == null ? team_win != null : !team_selected.equals(team_win)){//neu chon doi thua
                            /*
                             * Neu hoa thi chon doi bi chap se thang
                             * ex:ti so la 1-1 va ti le la 0:1
                             */
                            if(team_win == null ? "hoa" == null : team_win.equals("hoa"))
                            {
                                //neu chon doi bi chap
                                if(team_selected == null ? weakteam == null : team_selected.equals(weakteam))
                                {
                                   return (int) (price * rate_selected);
                                }
                                //Thua ca tien
                                else{
                                    return -price;
                                }
                            }

                            if(diff > first_par)
                            {
                                //thua ca
                                return -price;
                            } else if (diff == first_par) {
                                //hoa tien
                                return price;
                            } else if (diff < first_par) {
                                //thang tien tien
                                return (int) (price * rate_selected);
                            }
                        }
                    } else if(second[j] == null ? " 1/4" == null : second[j].equals(" 1/4")){

                        //Truong hop doi yeu hon thang
                        if(team_win == null ? weakteam == null : team_win.equals(weakteam))
                        {
                            if(team_selected == null ? team_win == null : team_selected.equals(team_win))
                            {
                                return (int) (price * rate_selected);
                            } else{
                                return -price;
                            }
                        }

                        if(team_selected == null ? team_win == null : team_selected.equals(team_win)){  //neu chon doi thang
                            if(diff >= first_par +1)
                            {
                                //thang ca
                                return (int) (price * rate_selected);
                            } else if (diff == first_par ) {
                                //thua nua tien
                                return price/2;
                            } else if (diff < first_par) {
                                //thua tien
                                return -price;
                            }

                        } else if(team_selected == null ? team_win != null : !team_selected.equals(team_win)){//neu chon doi thua

                            if(diff >= first_par +1)
                            {
                                //thua ca
                                return -price;
                            }
                            else if(team_win == null ? "hoa" == null : team_win.equals("hoa"))
                            {
                                //neu chon doi bi chap
                                if(team_selected == null ? weakteam == null : team_selected.equals(weakteam))
                                {
                                   return (int) (price * rate_selected);
                                }
                                //Thua ca tien
                                else{
                                    return -price;
                                }
                            }
                            else if (diff == first_par ) {
                                //thang nua tien
                                return (int) (price +price/2);
                            } else if (diff < first_par) {
                                //thang ca
                                return -(int) (price * rate_selected);
                            }
                        }

                    } else if(second[j] == null ? " 1/2" == null : second[j].equals(" 1/2")){

                            //Truong hop doi yeu hon thang
                            if(team_win == null ? weakteam == null : team_win.equals(weakteam))
                            {
                                if(team_selected == null ? team_win == null : team_selected.equals(team_win))
                                {
                                    return (int) (price * rate_selected);
                                } else{
                                    return -price;
                                }
                            }

                         if(team_selected == null ? team_win == null : team_selected.equals(team_win)){  //neu chon doi thang
                            if(diff >= first_par +1)
                            {
                                //thang ca
                                return (int) (price * rate_selected);
                            } else {
                                return -price;
                            }

                        } else if(team_selected == null ? team_win != null : !team_selected.equals(team_win)){//neu chon doi thua

                            if(diff >= first_par +1)
                            {
                                //thang ca
                                return -price;
                            }
                            else if(team_win == null ? "hoa" == null : team_win.equals("hoa"))
                            {
                                //neu chon doi bi chap
                                if(team_selected == null ? weakteam == null : team_selected.equals(weakteam))
                                {
                                   return (int) (price * rate_selected);
                                }
                                //Thua ca tien
                                else{
                                    return -price;
                                }
                            }
                            else{
                                
                                return (int) (price * rate_selected);//break
                            }
                        }
                        
                    }else if(second[j] == null ? " 3/4" == null : second[j].equals(" 3/4")){
                        //Truong hop doi yeu hon thang
                        //Truong hop doi yeu hon thang
                        if(team_win == null ? weakteam == null : team_win.equals(weakteam))
                        {
                            if(team_selected == null ? team_win == null : team_selected.equals(team_win))
                            {
                                return (int) (price * rate_selected);
                            } else{
                                return -price;
                            }
                        }

                        if(team_selected == null ? team_win == null : team_selected.equals(team_win)){  //neu chon doi thang
                            if(diff > first_par + 1)
                            {
                                //thang ca
                                return (int) (price * rate_selected);
                            }
                            else if (diff == first_par + 1) {
                                //thang nua su
                                return (price + price/2);
                            } else if (diff < first_par + 1) {
                                //thua tien
                                return -price;
                            }
                        } else if(team_selected == null ? team_win != null : !team_selected.equals(team_win)){//neu chon doi thua
                            if(diff > first_par + 1)
                            {
                                //thua ca
                                return -price;
                            }
                            else if(team_win == null ? "hoa" == null : team_win.equals("hoa"))
                            {
                                //neu chon doi bi chap
                                if(team_selected == null ? weakteam == null : team_selected.equals(weakteam))
                                {
                                   return (int) (price * rate_selected);
                                }
                                //Thua ca tien
                                else{
                                    return -price;
                                }
                            }
                            else if (diff == first_par + 1) {
                                //thang nua su
                                return (price + price/2);
                            } else if (diff <= first_par) {
                                //thang tien
                                return (int) (price * rate_selected);
                            }
                        }
                        
                    }
                }
            }

        }
        return valReturn;
    }

    public static StringBuffer getWeakTeamAndHigherRate(String[] arr,String rate_add)
    {
        StringBuffer strbuff = new StringBuffer();
        String[] arrstr = new String[10];
        String  weakteam = "";
        arrstr = rate_add.split(" : ");

        for (int i =0 ; i<arrstr.length ;i++)
        {
            if(arrstr[i] == null ? "0" != null : !arrstr[i].equals("0"))
            {
                weakteam = arr[i];
                strbuff.append(weakteam).append("-").append(arrstr[i]);
                break;
            }
        }
        return strbuff;
    }

    public static boolean handleDataTypeTX(int bet_id , int user_id, String team_selected,
                                                    String team_win,float rate_selected, int price,int total ,
                                                                                                String rate_add){
        boolean val_return = false;
        int value = getPriceTX(team_selected, rate_selected, total, rate_add, price);
        
        MySQLDataManager datamanager = new MySQLDataManager();        
        if(value > 0)
        {
            datamanager.updateCoinToTableBet(bet_id, value, 1);
            datamanager.updateCoinToTableUser(user_id, value, 1);
        } else {
            datamanager.updateCoinToTableBet(bet_id, value, -1);
            datamanager.updateCoinToTableUser(user_id, 0, -1);
        }
        val_return = true;
        return val_return;
    }
    public static int getPriceTX(String team_selected,float rate_selected, int total ,String rate_add ,int price)
    {
        int valReturn = 0;
        String first[] = new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        String second[] = new String[] {"", " 1/4", " 1/2", " 3/4"};

        for (int i = 0 ; i< first.length ; i++)
        {
            for(int j = 0 ; j< second.length ; j++)
            {
                if(rate_add == null ? first[i] + second[j] == null : rate_add.equals(first[i] + second[j]))
                {
                    if(team_selected == null ? "Tai" == null : team_selected.equals("Tai"))
                    {
                        valReturn = handleDataTai(price, total, first[i], second[j], rate_selected);
                        return valReturn;
                    } else if(team_selected == null ? "Xiu" == null : team_selected.equals("Xiu"))
                    {                       
                        valReturn =  handleDataXiu(price, total, first[i], second[j], rate_selected);
                        return valReturn; 
                    }
                    //if win,
                }
            }

        }
        return valReturn;
    }
    public static int handleDataTai(int price ,int total ,String first_val ,String second_val, float rate_selected){        
        //if win,cach biet 2 ban thang
                int strValue = 0;
                        //neu phan du bang null
                        if(second_val == null ? "" == null : second_val.equals(""))
                        {
                            //Neu tong ti so bang voi ti le dat thi tra lai tien
                            if(String.valueOf(total) == null ? first_val == null : String.valueOf(total).equals(first_val))
                            {                                
                                return price;
                            }
                            //Neu tong ti so lon hon thi thang
                            else if (total > Integer.valueOf(first_val)){
                                //return "thang";
                                return (int) (price * rate_selected);
                            }
                            //Neu tong ti so nho hon la thua
                            else if(total < Integer.valueOf(first_val)){
                                return -price;
                            }

                        }
                        //neu phan du bang 1/4
                        if(second_val == null ? " 1/4" == null : second_val.equals(" 1/4")){
                            //Neu hoa tong ti so  = phan nguyen thi thua nua
                            if(String.valueOf(total) == null ? first_val == null : String.valueOf(total).equals(first_val))
                            {                                
                                return (int) (price/2);
                            }
                            //Neu tong ti so lon hon phan nguyen thi thang ca
                            else if (total > Integer.valueOf(first_val)){
                                //return "thang";
                                return (int) (price * rate_selected);
                            } 
                            //Neu tong ti so nho hon phan nguyen thi thua ca
                            else if(total < Integer.valueOf(first_val)){
                                return -price;
                            }
                        }
                        //neu phan du bang 1/2
                        if(second_val == null ? " 1/2" == null : second_val.equals(" 1/2")){
                            //neu tong ti so lon hon phan nguyen thi thang ca
                            if(total > Integer.valueOf(first_val))
                            {                                
                                return (int) (price * rate_selected);
                            }
                            //Neu tong ti so <= phan nguyen thi thua 
                            else {                             
                                return -price;
                            }
                        }
                        //neu phan du bang 3/4
                        if(second_val == null ? " 3/4" == null : second_val.equals(" 3/4")){
                            //Neu tong ti so lon hon phan nguyen mot ban thi thang
                            if(total > Integer.valueOf(first_val) +1)
                            {                                
                                return (int) (price * rate_selected);
                            }
                            //neu tong ti so = voi phan nguyen +1 thi thang nua tieng
                            else if (total == Integer.valueOf(first_val) +1){
                                return (int) (price + price/2);
                            }
                            //Neu tong ti so <= hon phan nguyen thi thua
                            else {                                
                                return -price;
                            }
                        }
        
        return strValue;
    }
    public static int handleDataXiu(int price ,int total ,String first_val ,String second_val, float rate_selected){
        int strValue = 0;
        //Neu phan du bang null
        if(second_val == null ? "" == null : second_val.equals(""))
        {
            //Neu tong so bang thang bang voi phan nguyen thi tra lai tien
            if(String.valueOf(total) == null ? first_val == null : String.valueOf(total).equals(first_val))
            {                
                return price;
            }
            //Neu tong ti so nho hon phan nguyen thi thang
            else if (total < Integer.valueOf(first_val)){
                //return "thang";
                return (int) (price * rate_selected);
            }
            //Neu tong ti so lon hon phan nguyen thi thua
            else if(total > Integer.valueOf(first_val)){
                //return thua;
                return -price;
            }

        }
        //Neu phan du bang 1/4
        if(second_val == null ? " 1/4" == null : second_val.equals(" 1/4")){
            //Neu tong ti so bang voi phan nguyen thi thu thang nua tien
            if(String.valueOf(total) == null ? first_val == null : String.valueOf(total).equals(first_val))
            {                
                return (price + price/2);
            }
            //Neu tong ti so nho hon phan nguyen thi thang
            else if (total < Integer.valueOf(first_val)){
                return (int) (price * rate_selected);
            }
            //Neu tong ti so lon hon phan nguyen thi thua
            else if(total > Integer.valueOf(first_val)){
                return -price;
            }
        }
        //Neu phan du bang 1/2
        if(second_val == null ? " 1/2" == null : second_val.equals(" 1/2")){
            // Neu tong so bang thang nho hon hoac bang phan nguyen thi thang
            if(total <= Integer.valueOf(first_val))
            {                
                return (int) (price * rate_selected);
            }
            //Neu tong so bang thang lon hon phan nguyen mot ban thi thua
            else {                
                return -price;
            }
        }
        //Neu phan du bang 3/4
        if(second_val == null ? " 3/4" == null : second_val.equals(" 3/4")){
            //Neu tong so ban thang nho hon hoac bang 1 so voi phan nguyen thi thang
            if(total < Integer.valueOf(first_val) +1)
            {                
                return (int) (price * rate_selected);
            }
            //Neu tong so ban thang = voi phan nguyen +1 thi thua nua tien
            else if (total == Integer.valueOf(first_val) +1){                
                return (int)(price/2);
            }
            //Neu tong so ban thang lon hon phan nguyen 1 ban thi thua
            else {                
                return -price;
            }
        }

         return strValue;
        }
}
